<?php
namespace app\base\controller;
use app\rbac\model\AdminM;
use think\Db;
use think\Log;

class Index extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $this->assign(array(
            '_page_title'=>'首页',
            '_page_title_english'=>'Index',
            '_btn_title'=>'',
            '_btn_link'=>'',
            '_page_ico'=>'iconfa-laptop',
        ));
        return $this->fetch();
    }

    /**
     * 修改密码
     * @return \think\response\Json
     */
    public function setPassword()
    {
        $admin_id = session('admin_id');
        if($admin_id)
        {
            if($this->request->isPost())
            {//提交数据
                $data = $this->request->post();
                $model = new AdminM();
                $model->setPassword($data);
                return json(['code'=>$model->code,'msg'=>$model->msg]);
            }
            //未提交数据
            $this->assign(array(
                '_page_title'=>'修改密码',
                '_page_title_english'=>'set Password',
                '_btn_title'=>'',
                '_btn_link'=>'',
                '_page_ico'=>'iconfa-laptop',
            ));
           return  $this->fetch();
        }
        $this->redirect('base/Login/login');
    }

    /**
     * 编辑资料
     * @return \think\response\Json
     */
    public function editProfile()
    {
        $admin_id = session('admin_id');
        if($admin_id)
        {
            if($this->request->isPost())
            {//提交数据


//                $file = request()->file('ico');
//                // 移动到框架应用根目录/public/uploads/ 目录下
//                $info = $file->validate(['size'=>1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
//                if($info){
//                    $ico = $info->getSaveName();
//                }else{
//                    // 上传失败获取错误信息
//                    $this->error( $file->getError());
//                }
                $data = $this->request->post();
                $model = new AdminM();
                $res = $model->editProfile($data);
                if($res)
                {
                    $this->success('修改成功',url('base/Index/index'));
                }
                else
                {
                    $this->error('修改失败！');
                }
            }
            //未提交数据
            $data = Db::table('admin')->where('id',$admin_id)->field('admin_name,email,ico')->find();
            $this->assign(array(
                '_page_title'=>'修改资料',
                '_page_title_english'=>'edit Profile',
                '_btn_title'=>'',
                '_btn_link'=>'',
                '_page_ico'=>'iconfa-laptop',
                'data'=>$data,
            ));
            return  $this->fetch();
        }
        $this->redirect('base/Login/login');
    }

    /**
     * 测试七牛云上传
     * @return mixed
     */
    public function test()
    {
        return  $this->fetch();
    }
}
